import { isUrl } from '../utils/utils'

const menuData = [
  {
    name: '首页',
    icon: 'home',
    path: 'home',
    hideMenu: true
  },
  {
    name: '系统管理',
    icon: 'setting',
    path: 'setting',
    children: [{
      name: '仓库管理',
      path: 'warehouse'
    }, {
      name: '仓位管理',
      path: 'storePos'
    }, {
      name: '供应商管理',
      path: 'provider'
    }, {
      name: '计算单位管理',
      path: 'unit'
    }, {
      name: '产品类别',
      path: 'category'
    }]
  },
  {
    name: '权限管理',
    path: 'authorization',
    icon: 'book',
    children: [
      {
        name: '组件示例',
        path: 'demo'
      }
    ]
  },
  {
    name: '道具管理',
    icon: 'tool',
    path: 'tool',
    children: [
      {
        name: '组件示例',
        path: 'demo'
      }
    ]
  },
  {
    name: '业务操作',
    icon: 'file-text',
    path: 'business',
    children: [
      {
        name: '组件示例',
        path: 'demo'
      }
    ]
  },
  {
    name: '仓库作业',
    icon: 'bank',
    path: 'work',
    children: [
      {
        name: '组件示例',
        path: 'demo'
      }
    ]
  },
  {
    name: '收藏案例',
    icon: 'wallet',
    path: 'demo',
    hideMenu: true,
    children: [
      {
        name: '组件示例',
        path: 'demo'
      }
    ]
  },
  {
    name: '异常页',
    icon: 'warning',
    path: 'exception',
    hideMenu: true,
    children: [
      {
        name: '403',
        path: '403',
      },
      {
        name: '404',
        path: '404',
      },
      {
        name: '500',
        path: '500',
      }
    ],
  },
  {
    name: '账户',
    icon: 'user',
    path: 'user',
    authority: 'guest',
    children: [
      {
        name: '登录',
        path: 'login',
      }
    ],
  },
  {
    name: '登陆',
    icon: 'login',
    path: 'aa',
    authority: 'guest',
    children: [
      {
        name: '登录',
        path: 'bb',
      },
      {
        name: '注册',
        path: 'cc',
      },
      {
        name: '注册结果',
        path: 'cc',
      },
    ],
  }
]

function formatter(data, parentPath = '/', parentAuthority) {
  return data.map((item) => {
    let { path } = item
    if (!isUrl(path)) {
      path = parentPath + item.path
    }
    const result = {
      ...item,
      path,
      authority: item.authority || parentAuthority,
    }
    if (item.children) {
      result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority)
    }
    return result
  })
}

export const getMenuData = () => formatter(menuData)
